/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.splink.library.logging {
	import org.splink.library.logging.logoutput.DefaultOutputFormatter;
	import org.splink.library.logging.logoutput.ILogOutput;
	import org.splink.library.logging.logoutput.IOutputFormatter;
	import org.splink.library.utils.ArrayUtils;	

	/**
	 * Basic implementation of the <code>ILoggerFactory</code> interface.
	 * 
	 * @author Max Kugland
	 */
	public class LoggerFactory implements ILoggerFactory {
		private var _id : String = "default";
		private var _logOutputs : Array = new Array();
		private var _range : LogRange = new LogRange(LogLevel.TRACE, LogLevel.GARBAGE);
		private var _formatter : IOutputFormatter;
		private var _basePackage : String = "";

		public function LoggerFactory(id : String, formatter : IOutputFormatter = null) {
			_id = id;
			_formatter = formatter ? formatter : new DefaultOutputFormatter(id);
		}

		/**
		 * @inheritDoc
		 */
		public function set ouputFormatter(formatter : IOutputFormatter) : void {
			_formatter = formatter;
		}

		/**
		 * @inheritDoc
		 */
		public function set id(id : String) : void {
			_id = id;
		}

		/**
		 * @inheritDoc
		 */
		public function set range(range : LogRange) : void {
			_range = range;
		}

		/**
		 * @inheritDoc
		 */
		public function set basePackage(pack : String) : void {
			_basePackage = pack;
		}

		/**
		 * @inheritDoc
		 */
		public function addLogOutput(logOutput : ILogOutput) : void {
			if(ArrayUtils.contains(_logOutputs, logOutput)) return;
			logOutput.setOuputFormatter(_formatter);
			_logOutputs.push(logOutput);
		}

		/**
		 * @inheritDoc
		 */
		public function getLogger(clazz : Class) : ILogger {
			if(_logOutputs.length > 0) {
				var logger : ILogger = new Logger(_logOutputs, clazz, _id);
				logger.range = _range;
				logger.basePackage = _basePackage;
				return logger;
			}
			else throw new Error("You must add a LogOutput before you can get a Logger instance.");
		}

		/**
		 * @inheritDoc
		 */
		public function get id() : String {
			return _id;
		}
	}
}
